package com.cxs.texttospeech.slice;

import com.cxs.texttospeech.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.ai.tts.TtsClient;
import ohos.ai.tts.TtsListener;
import ohos.ai.tts.TtsParams;
import ohos.ai.tts.constants.TtsEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.PacMap;

import java.util.UUID;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");
    TextField infoText;
    Text textResult;
    private boolean initTtsResult;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        infoText = (TextField)findComponentById(ResourceTable.Id_text_info);
        textResult = (Text) findComponentById(ResourceTable.Id_text_result);
        Button btn_readText = (Button) findComponentById(ResourceTable.Id_btn_read_text);
        btn_readText.setClickedListener(this::readText);
        initTtsResult = false;
        TtsClient.getInstance().create(this, ttsListener);
    }

    private void readText(Component component) {
        if (initTtsResult) {
            HiLog.info(LABEL, "initItsResult is true");
            TtsClient.getInstance().speakText(infoText.getText(), null);
        } else {
            HiLog.info(LABEL, "initItsResult is false");
        }
    }

    private void showResult(String s) {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                textResult.append(s + "\n");
            }
        });
    }

    private TtsListener ttsListener = new TtsListener() {
        @Override
        public void onStart(String s) {
            showResult("onStart " + s);
        }

        @Override
        public void onProgress(String s, byte[] bytes, int i) {
            showResult("onProgress " + s + " i " + i);

        }

        @Override
        public void onFinish(String s) {
            showResult("onFinish " + s);
        }

        @Override
        public void onError(String s, String s1) {
            showResult("onError " + s + "s1 " + s1);
        }

        // Tts 客户端创建成功后， 会调用此方法
        @Override
        public void onEvent(int i, PacMap pacMap) {
            if (i == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) {
                TtsParams ttsParams = new TtsParams();
                ttsParams.setDeviceId(UUID.randomUUID().toString());
                initTtsResult = TtsClient.getInstance().init(ttsParams);
                showResult("onEvent " + initTtsResult);
            }
        }

        @Override
        public void onSpeechStart(String s) {
            showResult("onSpeechStart " + s);
        }

        @Override
        public void onSpeechProgressChanged(String s, int i) {
            showResult("onSpeechProgressChanged " + s);

        }

        @Override
        public void onSpeechFinish(String s) {
            showResult("onSpeechFinish " + s);

        }
    };
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
